package com.z6school.manage.service.impl;

import com.z6school.manage.common.annotation.RequiredLog;
import com.z6school.manage.common.pojo.CurrScore;
import com.z6school.manage.common.pojo.ScoreVo;
import com.z6school.manage.common.pojo.ScsVo;
import com.z6school.manage.mapper.CurriculumClassMapper;
import com.z6school.manage.mapper.CurriculumMapper;
import com.z6school.manage.mapper.ScoreMapper;
import com.z6school.manage.mapper.StudentMapper;
import com.z6school.manage.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private CurriculumMapper curriculumMapper;
    @Autowired
    private CurriculumClassMapper curriculumClassMapper;

    @Override
    @RequiredLog(operation = "查询成绩信息")
    public List<ScoreVo> findObjects(Integer classId) {
        //1.创建ScoreVo

        List<ScoreVo> scoreVos=new ArrayList<>();
        //查询该班级所有学生id
        List<Integer> studentIds = studentMapper.findStudentIds(classId);

        //填充ScoreVo类
        for (Integer id:studentIds) {
            //查询学生名
            String studentName = studentMapper.findNameById(id);
            ScoreVo scoreVo=new ScoreVo();
            scoreVo.setStudentId(id);
            scoreVo.setStudentName(studentName);
            //查询课程id与 分数 集合
            List<CurrScore> currScores = scoreMapper.findCurrScoreById(id);
            //定义总分 总学分
            double totalScore=0;
            double totalrealCredit=0;
            //循环得出总分，总学分
            for (CurrScore c:currScores) {

                //加取总分 总学分
                totalScore=totalScore+c.getScore();
                //获取该课程的学分值
                Double credit = curriculumMapper.findCreditByCurrId(c.getCurrId());
                //得出该学生的该课程的实际学分
                double realCredit =(c.getScore()/100)*credit;
               //小数点后取1位
                realCredit=Math.round(realCredit*10)/10.0;
                totalrealCredit=totalrealCredit+realCredit;

            }
            //向对象中添加总分数 总学分

            scoreVo.setTotalScore(totalScore);
            scoreVo.setTotalCredit(totalrealCredit);
            scoreVo.setCurrScores(currScores);

            scoreVos.add(scoreVo);

        }

        return scoreVos;
    }

    @Override
    public List<String> getCurrNames(Integer classId) {
        System.out.println(classId);
        List<Integer> studentIds = studentMapper.findStudentIds(classId);
        System.out.println(studentIds);
        Integer studentId = studentIds.get(0);
        System.out.println("studentId"+studentId);
        List<CurrScore> currScores = scoreMapper.findCurrScoreById(studentId);
        System.out.println("currScores"+currScores);
        List<String> currNames = new ArrayList<>();
        for (CurrScore c:currScores) {
            Integer currId = c.getCurrId();
            String currName = curriculumMapper.findNameById(currId);
            System.out.println("currnameInFor"+currName);
            currNames.add(currName);
        }
        System.out.println("impl"+currNames);
        return currNames;
    }

    @Override
    public ScoreVo  findOne(Integer studentId) {
        List<CurrScore> currScores = scoreMapper.findCurrScoreById(studentId);
        //定义总分 总学分
        double totalScore=0;
        double totalrealCredit=0;
        //循环得出总分，总学分
        for (CurrScore c:currScores) {

            //加取总分 总学分
            totalScore=totalScore+c.getScore();
            //获取该课程的学分值
            Double credit = curriculumMapper.findCreditByCurrId(c.getCurrId());
            //得出该学生的该课程的实际学分
            double realCredit =(c.getScore()/100)*credit;
            //小数点后取1位
            realCredit=Math.round(realCredit*10)/10.0;
            totalrealCredit=totalrealCredit+realCredit;

        }
        ScoreVo scoreVo=new ScoreVo();
        scoreVo.setTotalScore(totalScore);
        scoreVo.setTotalCredit(totalrealCredit);
        scoreVo.setCurrScores(currScores);
        return scoreVo;
    }

    @Override
    public String findNameByCurrId(Integer currId) {
        String name = curriculumMapper.findNameById(currId);

        return name;
    }

    @Override
    public void updateScores(ScsVo scsVo) {


        Integer[] currIds = scsVo.getCurrIds();
        Double[] scores = scsVo.getScores();
        List<Integer> studentIds = scoreMapper.findStudentIds();
        if (studentIds.contains(scsVo.getStudentId())){
            for (int i=0;i<currIds.length;i++){
                scoreMapper.updateBySidCid(scsVo.getStudentId(),currIds[i],scores[i]);
            }
        }else {
            for (int i=0;i<currIds.length;i++){
                scoreMapper.addScore(scsVo.getStudentId(),currIds[i],scores[i]);
            }
        }


    }

    @Override
    public List<Integer> getCurrIds(Integer studentId) {
        Integer classId = studentMapper.findClassIdByStudentId(studentId);
        List<Integer> list = curriculumClassMapper.findcurrIdsByClassId(classId);
        return list;
    }

    @Override
    public Integer getClassId(Integer studentId) {
        Integer classId = studentMapper.findClassIdByStudentId(studentId);
        return classId;
    }

    @Override
    public String getCurrName(Integer currId) {
        String currName = curriculumMapper.findNameById(currId);
        return currName;
    }
}
